#include "UI\Controls\ControlFactory.h"
#include "Base\Settings.h"
#include "Base\Surface.h"
#include "Base\Engine.h"

///////////////////////////////////////////////////////////
//        ///  //////  /        ///  ///////  ///        //
//  //////  /  //////  /  //////  /  ///////  /  //////////
//        ///  //////  /        ///  ///////  /  //////////
//  /////////  //////  /  //////  /  ///////  /  //////////
//  ///////////      ///        ///        /  ///        //
///////////////////////////////////////////////////////////

//bool ShRp::Base::Engine::Init(HINSTANCE _HInstance, HINSTANCE _HPrevInstance, LPSTR _LpCmdLine, int _nCmdShow)
bool ShRp::Base::Engine::Init(int _NumArgs, char ** _Args)
{
	printf("DEBUG: Engine Initializing.\n");
	m_Timer.Start();
	if(!Util::Logger::Init())			{	printf("ERROR: Logger::Init() failed.\n");	BREAK();	return false;	}
	if(!Squirrel::Init())				{	Error("Squirrel::Init() failed.\n");					return false;	}
	if(!Engine::InitGDIPlus())			{	Error("Engine::InitGDIPlus() failed.\n");				return false;	}

	if(!UI::ControlMgr::Init())			{	Error("ControlMgr::Init() failed.\n");					return false;	}
	if(!UI::ControlFactory::Init())		{	Error("ControlFactory::Init() failed.\n");				return false;	}

	if(!Settings::Init())				{	Error("Settings::Init() failed.\n");					return false;	}
	if(!Surface::Init())				{	Error("Surface::Init() failed.\n");						return false;	}

	Settings::Load("settings.cfg");
	Color fontColor = Settings::GetSetting<Color>("fontColor");
	Color bgColor = Settings::GetSetting<Color>("bgColor");
	Base::Surface::SetClearColor(bgColor);
	Color foreColor = Settings::GetSetting<Color>("foreColor");
	Color foreColor2 = Settings::GetSetting<Color>("foreColor2");
	std::string str = Settings::GetSetting<std::string>("test");
	str[4] = 'd';
	str = Settings::GetSetting<std::string>("test");
	Settings::Save("settingsSaved.cfg");
	

	Surface::SetFont("Times New Roman", 8);

	UI::Controls::SimpleControl* wnd = (UI::Controls::SimpleControl*)UI::ControlFactory::CreateControl(UI::Control::Type::SimpleControl);
	wnd->Init(int2(10, 10), int2(100, 100));

	Log("Engine Initialized.\n");
	return true;
}

void ShRp::Base::Engine::Tick()
{
	MSG msg;
	WaitMessage();
	while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);

	}
}

void ShRp::Base::Engine::Terminate()
{
	Surface::Terminate();
	Settings::Terminate();
	UI::ControlFactory::Terminate();
	UI::ControlMgr::Terminate();
	Engine::TerminateGDIPlus();
	Squirrel::Terminate();
	Util::Logger::Terminate();
}

bool ShRp::Base::Engine::InitGDIPlus()
{
	// Initialize GDI+.
	Gdiplus::GdiplusStartupInput gdiplusStartupInput;
	Gdiplus::GdiplusStartup(&m_GDIplusToken, &gdiplusStartupInput, NULL);

	return true;
}

void ShRp::Base::Engine::TerminateGDIPlus()
{
	// Terminate GDI+.
	Gdiplus::GdiplusShutdown(m_GDIplusToken);
}

float ShRp::Base::Engine::GetCurRunTime()
{
	return max(0, m_Timer.GetPassedTime());
}

uint ShRp::Base::Engine::GetSystemTime()
{
	return (uint)time(0);
}
////////////////////////////////////////////////////////////////
//      ///      ///  /  //////  ///    ///          /        //
//  ////  /  ////  /  /  //////  /  ////  /////  /////  ////////
//      ///      ///  ///  //  ///        /////  /////      ////
//  ///////  //  ///  ///  //  ///  ////  /////  /////  ////////
//  ///////  ////  /  /////  /////  ////  /////  /////        //
////////////////////////////////////////////////////////////////

ULONG_PTR ShRp::Base::Engine::m_GDIplusToken;
Timer ShRp::Base::Engine::m_Timer;
